#!/bin/bash
time_ss=`date "+%Y-%m-%d %H:%M:%S"`
# dizhi_="$2" #执行文件地址

dizhi_2="$2"

dizhi_1=$(find ${dizhi_2} -maxdepth 1 -type d -name "20*"|sort) 

for dizhi_ in $dizhi_1   #0.文件夹处理 -------------------------
do

dizhi_=${dizhi_}"/"

wen_=$(find ${dizhi_} -maxdepth 1 -type d -name "hk_erp2*" -o -name "finedb"|sort) 

for dizhi in $wen_   #1.文件夹处理 -------------------------
do
if [[ $dizhi =~ "finedb" ]]
then
    data='finedb'
else
    xiugai="${dizhi_}hk_erp2_"
    data=$(echo "$dizhi"|sed "s#$xiugai##g")
	data="\_$data"
fi

server_="$1"  #实例

for server_1 in $server_  #2.执行的数据库实例 ------------------------------------------------------------start
do
#获取对应的mysql连接串
lq_d=$(bash /mnt/f/mysql/sh/lq-xml $server_1)
#echo $lq_d

if [ "$lq_d" != "" ]
then

if [ "$1" == "g2" ]
then
case $3 in 
1)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "fs\_|fy\_") #执行的数据库
;;
2)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "qgzz\_|zclx\_") #执行的数据库
;;
3)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "pekd\_|sme3\_") #执行的数据库
;;
4)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme4\_|sme5\_|\_tenant|zj\_hk") #执行的数据库
;;
5)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "dk\_|kls\_") #执行的数据库
;;
6)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme2\_|sme6\_")  #执行的数据库
;;
7)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "361\_|cdws\_") #执行的数据库
;;
8)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E "sme\_") #执行的数据库
;;
*)
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database") #执行的数据库
;;
esac
else 
db_=$(mysql --defaults-extra-file=/mnt/f/mysql/sh/$server_1 -e "show databases like '%$data';"|grep -v "Database"|grep -E 'hk_erp2|finedb') #执行的数据库
fi

xml_=$(find ${dizhi} -maxdepth 1 -type f -name 'V*.xml'|sort)

for xml_zx in $xml_   #3.xml执行处理 ------------------------- start
do

xml_zz=$(echo "$xml_zx"|sed "s#${dizhi}/##g")


for db_1 in $db_ #4.数据库执行 ------------------------------------------------
do

cc=$(mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"SELECT CONCAT('db-',LPAD(IFNULL(MAX(id),0)+1, 9, '0')) AS bill_no FROM dba_log;"|grep -v "bill_no")

cc_zhi="${dizhi}/${xml_zz}"

id_=$(mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"INSERT INTO dba_log(server_ip,database_name,guid, dizhi, msg,create_time)VALUE('$server_1','$db_1','$cc','$cc_zhi','',now(3));SELECT LAST_INSERT_ID() AS id"|grep -v "id");
log1="${db_1}.log"

liquibase_zx="cd $dizhi;>${log1};
liquibase --driver=com.mysql.cj.jdbc.Driver ${lq_d}${db_1}?useCompression=true \
--changeLogFile=${xml_zz} --log-file=${log1} --log-level=SEVERE update;"

#liquibase 执行
bash -c "$liquibase_zx"
time_=`date +%Y-%m-%d\ %H:%M:%S.%3N`
       
log12="${dizhi}/${db_1}.log"
msg=$(cat ${log12}) #问题排查
if [ "$msg" != "" ]
then
sql_='update dba_log set msg="'$(cat ${log12})'" ,stats=1,modified_time="'$time_'" where id='$id_' ;'
mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"$sql_"
else
mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"update dba_log set modified_time='$time_' where id=$id_ ;"
fi

done    #4.数据库执行 ------------------------------------------------  liquibase --log-level  --help
done     #3.xml执行处理 ------------------------- end
else
echo "${server_1}连接不在 mnt/e/sh/lq-xml 里面"
fi
done   #2.执行的数据库实例 ------------------------------------------------------------end

done #1.文件夹处理 -------------------------

done #0.文件夹处理 -------------------------

mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"DELETE FROM dba_log WHERE create_time<DATE_ADD(NOW(),INTERVAL -6 MONTH)";

#mysql --defaults-extra-file=/mnt/e/mysql/sh/test -D hk_dba -e"call sp_zhix('$time_ss')">/mnt/e/sh/log/mysqlzxerror
